<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

function b_login()
{
	$name_function = "b_login";

	global $a, $db, $blocks_db, $template, $language, $config, $misc, $PHP_SELF,
           $is_loged_in, $is_moderator, $is_administrator, $is_webmaster,
           $months, $days, $m, $y, $OnlineGuestsCount, $OnlineMembersCount,
           $sitelanguage;
    
    if (!file_exists("blocks/$name_function/language/$sitelanguage.php"))
        include "blocks/$name_function/language/en.php";
	else
        include "blocks/$name_function/language/$sitelanguage.php";

	$random_code = md5("$name_function");
	$random_code = substr("$random_code", 0, 5);

	if (!isset($content))
        $content = NULL;
	if (!isset($_GET["$random_code"]))
        $_GET["$random_code"] = NULL;

	if ($_GET["$random_code"] == "on")
        $_SESSION["$random_code"] = "on";
	elseif ($_GET["$random_code"] == "off")
        $_SESSION["$random_code"] = "off";

	$query_string = "?{$_SERVER['QUERY_STRING']}";

	foreach($blocks_db as $blocks)
	{
		$block_code = md5("{$blocks['name']}");
		$block_code = substr("$block_code", 0, 5);

		$query_string = str_replace("&amp;$block_code=off", "", $query_string);
		$query_string = str_replace("&amp;$block_code=on", "", $query_string);
	}

	if ($query_string == "?" ||
        strpos($query_string, '=on') !== false ||
        strpos($query_string, '=off') !== false)
        $code_query = "?";
	else
        $code_query = "$query_string&amp;";

    if (!isset($is_loged_in))
	{
    	if (!isset($_SESSION["$random_code"]) ||
            $_SESSION["$random_code"] == "on")
            $links = "<a href='$code_query$random_code=off'>".
            "<img src='templates/$template/images/misc/minus.gif' border='0' ".
            "alt='minus'> {$language_block['Login title']}</a>";
    	elseif ($_SESSION["$random_code"] == "off")
            $links = "<a href='$code_query$random_code=on'>".
            "<img src='templates/$template/images/misc/plus.gif' border='0' ".
            "alt='plus'> {$language_block['Login title']}</a>";
    }
    else
    {
    	if (!isset($_SESSION["$random_code"]) ||
            $_SESSION["$random_code"] == "on")
            $links = "<a href='$code_query$random_code=off'>".
            "<img src='templates/$template/images/misc/minus.gif' border='0' ".
            "alt='minus'> {$language_block['Control title']}</a>";
    	elseif ($_SESSION["$random_code"] == "off")
            $links = "<a href='$code_query$random_code=on'>".
            "<img src='templates/$template/images/misc/plus.gif' border='0' ".
            "alt='plus'> {$language_block['Control title']}</a>";
    }

	$template_block = implode("", file("templates/$template/block.php"));
	$template_block = str_replace("{template}", $template, $template_block);
	$template_block = str_replace("{links}", $links, $template_block);

	if (!isset($_SESSION["$random_code"]) ||
        $_SESSION["$random_code"] == "on")
	{
		//-----------------------------------------------------------------
		//  Show Login Form
		//-----------------------------------------------------------------

		if(!isset($is_loged_in))
		{
			$content .=
            "<tr>".
            "<td class='con1' align='right' width='50%'>".
            "{$language_block['Login username']}</td>".
			"<td class='con1' width='50%'><input type='text' name='username' ".
            "style='width:100%' maxlength='25' class='box' tabindex='1'></td>".
            "</tr>".
            "<tr>".
            "<td class='con1' align='right'>".
            "{$language_block['Login password']}</td>".
            "<td class='con1'><input type='password' name='password' ".
            "style='width:100%' maxlength='25' class='box' tabindex='2'></td>".
            "</tr>".
            "<tr>".
            "<td class='con1' align='right'><input type='button' ".
            "name='register' style='width:100%' class='box' ".
            "value='{$language_block['Login register']}' ".
            "onclick='window.location.href=\"$PHP_SELF?n=modules/register\"' ".
            "tabindex='4'></td>".
            "<td class='con1' align='left'><input accesskey='s' type='submit' ".
            "name='submit' style='width:100%' class='box' ".
            "value='{$language_block['Login submit']}' tabindex='3'></td>".
            "</tr>".
            "<tr>".
            "<td class='con1' align='center' colspan='2' ".
            "onMouseOver='this.className=con3' ".
            "onMouseOut='this.className=con1' ".
            "onclick='window.location.href=$PHP_SELF?n=modules/register&amp;".
            "a=3'>".
            "<a href='$PHP_SELF?n=modules/register&amp;a=3'>".
            "{$language_block['Login forgotten']}</a></td>".
            "</tr>";
		}

		//-----------------------------------------------------------------
		//  Show Control Panel
		//-----------------------------------------------------------------

		else
		{
			//-----------------------------------------------------------------
			//  Check see if he has acces to admin area
			//-----------------------------------------------------------------

			if ($is_moderator || $is_administrator || $is_webmaster)
                $admin_area = "<tr>".
                "<td class='con1' onMouseOver='this.className=con3' ".
                "onMouseOut='this.className=con1' ".
                "onclick='window.location.href=$PHP_SELF?n=modules/admin'>".
                "<a href='$PHP_SELF?n=modules/admin'> &raquo; ".
                "{$language_block['Control admin']}</a></td>".
                "</tr>";
			else
                $admin_area = NULL;

			//-----------------------------------------------------------------
			//  Calculate PM's + color
			//-----------------------------------------------------------------

			$new_pm_count = $db->getInfo_countRows("pm",
            "r_user_id = '{$_SESSION['id']}' AND ".
            "r_user_user = '{$_SESSION['username']}' AND r_date = 'unread' ".
            "AND r_delete = 1");

			if (!isset($change_color)) $change_color = NULL;
			if ($new_pm_count != 0) $change_color = 1;

			//-----------------------------------------------------------------
			//  Show HTML
			//-----------------------------------------------------------------

			$content .= "$admin_area".
            "<tr>".
            "<td class='con1' onMouseOver='this.className=con3' ".
            "onMouseOut='this.className=con1' ".
            "onclick='window.location.href=$PHP_SELF?n=modules/hq'>".
            "<a href='$PHP_SELF?n=modules/hq'> &raquo; ".
            "{$language_block['Control headquarters']}</a></td>".
            "</tr>".
			"<tr>".
            "<td class='con1' onMouseOver='this.className=con3' ".
            "onMouseOut='this.className=con1' ".
            "onclick='window.location.href=$PHP_SELF?n=modules/pm'>".
            "<a href='$PHP_SELF?n=modules/pm'>";
            
            if ($change_color == "1")
                $content .= "<font color='red'>";
            
            $content .= " &raquo; {$language_block['Control private']} ".
            "($new_pm_count {$language_block['Control private2']})";
            
            if ($change_color == "1")
                $content .= "</font>";
            
            $content .= "</a></td>".
            "</tr>".
			"<tr>".
            "<td class='con1' onMouseOver='this.className=con3' ".
            "onMouseOut='this.className=con1' ".
            "onclick='window.location.href=$PHP_SELF?n=modules/events&amp;".
            "a=4&amp;d=".date("j")."&amp;m=".date("n")."'>".
            "<a href='$PHP_SELF?n=modules/events&amp;a=4&amp;".
            "d=".date("j")."&amp;m=".date("n")."'> &raquo; ".
            "{$language_block['Control event']}</a></td>".
            "</tr>".
			"<tr>".
            "<td class='con1' onMouseOver='this.className=con3' ".
            "onMouseOut='this.className=con1' ".
            "onclick='window.location.href=$PHP_SELF?n=modules/panel'>".
            "<a href='$PHP_SELF?n=modules/panel'> &raquo; ".
            "{$language_block['Control panel']}</a></td>".
            "</tr>".
			"<tr>".
            "<td class='con1' onMouseOver='this.className=con3' ".
            "onMouseOut='this.className=con1' ".
            "onclick='window.location.href=$PHP_SELF?n=modules/login&amp;a=1'>".
            "<a href='$PHP_SELF?n=modules/login&amp;a=1'> &raquo; ".
            "{$language_block['Control logout']}</a></td>".
            "</tr>";
		}
	}

	$template_block = str_replace("{content}", $content, $template_block);
	$output = "<form method='post' action='$PHP_SELF?n=modules/login'>
    $template_block</form>";

return $output;
}

?>